'''
case Type :数据库系统
case Name ：人为误加主备机配置文件，导致主备配置文件不一致，影响数据库重启
Create At: 2020-7-8
owner : @peilinqian
description:1、主机增加不存在参数2、重启主机失败3、恢复主机配置参数，重启成功
history：
created： 2020/07/08  created by @peilinqian
'''

import os
import time
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Common import *


class MyTest(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case020.py start------------')
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.postgresqlpath = os.path.join(macro.DB_INSTANCE_PATH, macro.DB_PG_CONFIG_NAME)
        self.rootNode = Node(node='PrimaryRoot')
        self.dbUserNode = Node(node='PrimaryDbUser')
        self.standbyrootNode = Node(node='Standby1Root')
        self.standbydbUserNode = Node(node='Standby1DbUser')
        self.com = Common()
        self.valuestr = "aaaa = 123123"

    def test_main(self):
        # 主机添加不相关参数行
        self.log.info('add inexistent value')
        add_value_cmd = "echo '{value}' >> {path}".format(value=self.valuestr, path=self.postgresqlpath)
        self.dbUserNode.sh(add_value_cmd)

        # kill主机
        msg = self.com.kill_pid(self.rootNode, 9)
        self.assertEqual(msg, '')

        # 主机重启数据库,失败
        is_started = self.primary_sh.startDbCluster()
        self.assertFalse(is_started)

    def tearDown(self):
        # 删除不相关参数行
        self.log.info('del inexistent value')
        del_value_cmd = "sed -i '/{value}/d'  {path}".format(value=self.valuestr, path=self.postgresqlpath)
        self.log.info(del_value_cmd)
        self.dbUserNode.sh(del_value_cmd)

        value = self.com.getFileStringRows(self.dbUserNode, self.postgresqlpath, self.valuestr)
        self.log.info(value)

        # 主机重启数据库,成功
        is_started = self.primary_sh.startDbCluster()
        self.assertTrue(is_started)
